package com.zwps.biz.controller.http;



import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.*;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class Test {
        public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://10.34.186.151:3306/rsvrmanage_longhekou"; // 替换为你的数据库名
        String user = "root"; // 替换为你的数据库用户名
        String filePath; // 替换为你的 JSON 文件路径
        String password = "lkhsK@2024"; // 替换为你的数据库密码
        String a1 = "";
        try (Connection conn = DriverManager.getConnection(url, user, password)) {
        for (int i = 1; i <26 ; i++) {
            if (i<10){
                a1="000"+i;
            }else if (i<100){
                a1="00"+i;
            }
            filePath = "E:\\快手素材\\08溃坝过程数据\\08溃坝过程数据\\10000\\每小时淹没地物json\\下游人口\\results_t_"+a1+"_wet_area下游人口.json";
            try {
                a(conn,filePath,i);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }


        // JDBC 连接




        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void   a(Connection conn,String filePath,Integer time){
        // 读取 JSON 文件

        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(new JSONTokener(new FileReader(filePath)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        JSONArray features = jsonObject.getJSONArray("features");

        // 插入数据到数据库
        String sql = "INSERT INTO lhk_downstream_submergence_copy1 (  type,time,waterlevel,village,status,level,town,population) " +
                "VALUES (?, ?, ?, ?,?,?,?,?)";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            for (int i = 0; i < features.length(); i++) {
                JSONObject feature = features.getJSONObject(i);
                JSONObject attributes = feature.getJSONObject("attributes");
                JSONObject geometry = feature.getJSONObject("geometry");


//

                String village = attributes.get("CMC").toString();
                String town = attributes.get("XZMC").toString();
//                BigDecimal area = attributes.getBigDecimal("CZCMJ");
                int population=attributes.getInt("Count");
//
                pstmt.setInt(1, 4);
                pstmt.setInt(2, time);
                pstmt.setInt(3, 10000);
                pstmt.setString(4, village);
                pstmt.setInt(5, 4);
                pstmt.setBigDecimal(6, new BigDecimal(8.03));
                pstmt.setString(7, town);
                pstmt.setInt(8,population);
//                pstmt.setBigDecimal(8, area);



                pstmt.addBatch(); // 添加到批处理
            }
            pstmt.executeBatch(); // 执行批处理
            System.out.println("数据插入成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
//    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/lkhsk";
//    private static final String USERNAME = "root";
//    private static final String PASSWORD = "123456";
//
//    public static void main(String[] args) {
//        Test test = new Test();
//        test.importFromFile("D:\\飞书\\download\\杭埠河断面横断面_间距文件.txt");
//    }
//    public  void importFromFile(String filePath) {
//        try (Connection connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
//             BufferedReader reader = new BufferedReader(
//                     new InputStreamReader(new FileInputStream(filePath), "GBK"))) { // 使用GBK编码读取
//
//            List<CrossSection> sections = parseFile(reader);
//            insertData(connection, sections);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("导入失败: " + e.getMessage());
//        }
//    }
//
//    private static List<CrossSection> parseFile(BufferedReader reader) throws Exception {
//        List<CrossSection> sections = new ArrayList<>();
//        CrossSection currentSection = null;
//        String line;
//        int sectionCount = 1;
//
//        while ((line = reader.readLine()) != null) {
//            line = line.trim();
//            if (line.isEmpty()) continue;
//
//            String[] parts = line.split(",");
//
//            if (parts[0].equals("桩号")) {
//                // 保存前一个断面（如果有）
//                if (currentSection != null) {
//                    sections.add(currentSection);
//                }
//                // 创建新断面
//                currentSection = new CrossSection();
//                currentSection.sccd = "断面" + sectionCount++;
//                currentSection.sccdCode = parts[1];
//                currentSection.points = new ArrayList<>();
//            }
//            else if (currentSection != null) {
//                if (parts[0].equals("左侧") && parts.length >= 3) {
//                    currentSection.points.add(new Point(parts[1], parts[2], "left"));
//                }
//                else if (parts[0].equals("右侧")) {
//                    for (int i = 1; i < parts.length - 1; i += 2) {
//                        if (i + 1 < parts.length) {
//                            currentSection.points.add(new Point(parts[i], parts[i + 1], "right"));
//                        }
//                    }
//                }
//            }
//        }
//
//        // 添加最后一个断面
//        if (currentSection != null) {
//            sections.add(currentSection);
//        }
//
//        return sections;
//    }
//
//    private static void insertData(Connection connection, List<CrossSection> sections) throws Exception {
//        String sql = "INSERT INTO cross_section (sccd, sccdCode, x, y) VALUES (?, ?, ?, ?)";
//
//        try (PreparedStatement statement = connection.prepareStatement(sql)) {
//            connection.setAutoCommit(false);
//            int totalPoints = 0;
//
//            for (CrossSection section : sections) {
//                for (Point point : section.points) {
//                    statement.setString(1, section.sccd);
//                    statement.setString(2, section.sccdCode);
//                    statement.setString(3, point.x);
//                    statement.setString(4, point.y);
//                    statement.addBatch();
//                    totalPoints++;
//                }
//            }
//
//            int[] results = statement.executeBatch();
//            connection.commit();
//            System.out.println("成功插入 " + sections.size() + " 个断面，共 " + totalPoints + " 个点");
//
//            // 打印统计信息
//            for (CrossSection section : sections) {
//                System.out.println("断面: " + section.sccd + " (" + section.sccdCode + "), 点数: " + section.points.size());
//            }
//        }
//    }
//
//    // 内部类：断面
//    static class CrossSection {
//        String sccd;        // 断面编号（断面1、断面2...）
//        String sccdCode;    // 断面编码（K00+0.000、K01+876.306）
//        List<Point> points; // 断面点列表
//    }
//
//    // 内部类：点
//    static class Point {
//        String x;
//        String y;
//        String side;
//
//        public Point(String x, String y, String side) {
//            this.x = x;
//            this.y = y;
//            this.side = side;
//        }
//    }
}
